package com.example.controller;

import com.example.common.R;
import com.example.entity.Ban;
import com.example.entity.Page;
import com.example.service.BanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("bans")
public class BanController {

    private R r;

    @Autowired
    private BanService banService;
    

    /**
     * 分页查询所有管理员信息
     * @return
     */

    @GetMapping("/getPageBan/{currentPage}/{pageSize}")

    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){

        List<Ban> page = banService.getPage(currentPage,pageSize);
        int count = banService.getTotalCount();

        return R.success("分页查询所有admins成功！",new Page<Ban>(count,page));
    }

    /**
     * 查询所有班号
     * @return
     */

    @GetMapping("/getAllBanNumber")

    public R getAllBanNumber(){

        List<Ban> banList = banService.getAllBanNumber();

        return R.success("分页查询所有admins成功！",banList);
    }



    /**
     * 添加
     * @param ban
     * @return
     */
    @PostMapping("/addAdmin")
    public R add(@RequestBody Ban ban){

        Boolean update = banService.add(ban);
        if (update){
            return R.success("添加admin成功!",null);
        }
        return R.error("添加admin失败!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBan/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = banService.deleteById(id);
        if (byId){
            return R.success("删除admins成功!",null);
        }
        return R.error("删除admins失败!");
    }

    /**
     * 修改
     * @param ban
     * @return
     */
    @PutMapping("/updateAdmin")
    public R deleteById(@RequestBody Ban ban){

        Boolean update = banService.update(ban);
        if (update){
            return R.success("修改admin成功!",null);
        }
        return R.error("修改admin失败!");
    }


}

